<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ include file="/systeminfo/init_wev8.jsp" %>
<jsp:useBean id="ResourceComInfo" class="weaver.hrm.resource.ResourceComInfo" scope="page"/>
<%@ taglib uri="/browserTag" prefix="brow"%>
<%@ taglib uri="/WEB-INF/weaver.tld" prefix="wea"%>
<%@ page import="com.engine.kq.biz.*" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="com.alibaba.fastjson.JSON" %>
<%@ page import="com.engine.kq.entity.WorkTimeEntity" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.workflow.field.BrowserComInfo" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="com.alibaba.fastjson.serializer.SerializerFeature" %>
<%@ page import="weaver.file.Prop" %>
<%@ page import="java.net.InetAddress" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.net.URLConnection" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="com.ibm.icu.text.SimpleDateFormat" %>
<%@ page import="java.text.ParseException" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="static com.weavernorth.util.MyCommonUtils.getUseridByCode" %>
<%@ page import="java.util.Date" %>
<%@ page import="weaver.conn.RecordSetTrans" %>
<%
    String strUserid = Util.null2String(request.getParameter("resourceid"));//人员id
    String today = DateUtil.getCurrentDate();
    String strBegin = Util.null2String(request.getParameter("kqdate"),today);//开始日期
    String strEnd = Util.null2String(request.getParameter("kqdateto"),today);//结束日期
    String strTime_start = Util.null2String(request.getParameter("signTime"));//签到时间
    String strTime_end = Util.null2String(request.getParameter("signOutTime"));//签退时间
    String strRequestids = Util.null2String(request.getParameter("requestid"));//流程id
    String strCheck = Util.null2String(request.getParameter("check"));//是否插入签退记录
    String strIsV8 = Util.null2String(request.getParameter("siV8"));//是否V8打卡
    String cmd = Util.null2String(request.getParameter("cmd"));
    Map<String,Object> msg=new HashMap<>();
    //获取考勤明细
    String strSql_kq = "SELECT kqdate,signindate,signintime,signoutdate,signouttime,attendancemins,signmins,belatemins,graveBeLateMins,leaveearlymins,graveLeaveEarlyMins,absenteeismmins,forgotcheckMins FROM kq_format_detail WHERE kqdate>='" + strBegin + "' AND kqdate<='" + strEnd + "' AND resourceid=" + strUserid + "";
    RecordSet rs_format_detail = new RecordSet();
    rs_format_detail.executeQuery(strSql_kq);
    String strkqdate = "";
    String strCdmins = "";
    String strCdmins_yz = "";
    String strZtmins = "";
    String strKgmins = "";
    String strCqsc = "";
    String strSignmins = "";
    String strSignintime = "";//签到时间
    String strSignouttime = "";//签到时间
    String strLqmins="";
    String strSql = "";//更新语句
    if(cmd.equals("update")) {
        if(!"on".equals(strIsV8)) {
            while (rs_format_detail.next()) {
                //考勤日期
                strkqdate = Util.null2String(rs_format_detail.getString("kqdate"));
                //签到时间
                strSignintime = Util.null2String(rs_format_detail.getString("signintime"));
                //签退时间
                strSignouttime = Util.null2String(rs_format_detail.getString("signouttime"));
                //迟到时长
                strCdmins = Util.null2String(rs_format_detail.getString("belatemins"));
                //严重迟到时长
                strCdmins_yz = Util.null2String(rs_format_detail.getString("graveBeLateMins"));
                //早退时长
                strZtmins = Util.null2String(rs_format_detail.getString("leaveearlymins"));
                //严重早退时长
                String strZtmins_yz = Util.null2String(rs_format_detail.getString("graveLeaveEarlyMins"));
                //旷工时长
                strKgmins = Util.null2String(rs_format_detail.getString("absenteeismmins"));
                //漏签时长
                strLqmins = Util.null2String(rs_format_detail.getString("forgotcheckMins"));
                //应出勤时长
                strCqsc = Util.null2String(rs_format_detail.getString("attendancemins"));
                //签到时长
                strSignmins = Util.null2String(rs_format_detail.getString("signmins"));
                msg.put("原始数据：", "-------");
                msg.put("考勤日期：", strkqdate);
                msg.put("签到时间：", strSignintime);
                msg.put("签退时间：", strSignouttime);
                msg.put("迟到时长：", strCdmins);
                msg.put("旷工时长：", strKgmins);
                msg.put("漏签时长：", strLqmins);
                msg.put("应出勤时长：", strCqsc);
                msg.put("签到时长：", strSignmins);
                if (strSignintime.equals("")) {//签到时间为空时
                    if (!strTime_start.equals("")) {
                        strSignintime = strTime_start;
                        String strIsExists = "select * from hrmschedulesign where signdate='" + strkqdate + "' and userid='"+strUserid+"' and signtype=1";
                        String strIntId = "";
                        RecordSet rs_isexists = new RecordSet();
                        rs_isexists.execute(strIsExists);
                        while (rs_isexists.next()){
                            strIntId=rs_isexists.getString("id");
                        }
                        if (rs_isexists.getCounts() < 1) {//不存在则插入
                            String strInsertSignIn = "INSERT into hrmschedulesign (userid,usertype,SIGNTYPE,signdate,signtime,CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,BELONGDATE,MEMO,deviceinfo)" +
                                    "SELECT userid,usertype,1,'" + strBegin + "','" + strSignintime + "',CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,'" + strkqdate + "',MEMO,deviceinfo FROM hrmschedulesign WHERE USERID='" + strUserid + "' AND signdate='2020-11-20' AND signtype=1";
                            RecordSet rs_signIn = new RecordSet();
                            rs_signIn.execute(strInsertSignIn);
                            msg.put("插入签到Sql：", strInsertSignIn);
                            msg.put("check：", strCheck);

                            RecordSet rs_getKey = new RecordSet();
                            String strId = "";
                            String strGetId = "select id from hrmschedulesign where signdate='" + strBegin + "' and userid='"+strUserid+"' and signtype=1 and rownum=1";
                            rs_getKey.execute(strGetId);
                            while (rs_getKey.next()) {
                                strId = rs_getKey.getString("id");
                            }
                            RecordSet rs_update_detail = new RecordSet();
                            String str_update_detail = "update kq_format_detail set signinid='" + strId + "',signdate='" + strBegin + "',signintime='" + strSignintime + "',attendancemins=480,belatemins=0,graveBeLateMins=0,leaveearlymins=0,graveLeaveEarlyMins=0,absenteeismmins=0,forgotcheckMins=0 where kqdate='" + strBegin + "' and resourceid='" + strUserid + "'";
                            rs_update_detail.execute(str_update_detail);
                            msg.put("更新签到明细表Sql：", str_update_detail);
                        }else{
                            String strUpdateSign = "UPDATE hrmschedulesign SET isincom=1,signtime='" + strSignintime + "' where userid='" + strUserid + "' and signtype=1 and signdate='" + strkqdate + "'";
                            RecordSet rs_signIn = new RecordSet();
                            rs_signIn.execute(strUpdateSign);
                            msg.put("更新签到Sql：", strUpdateSign);
                            RecordSet rs_getKey = new RecordSet();
                            String strId = "";
                            String strGetId = "select id from hrmschedulesign where signdate='" + strBegin + "' and userid='"+strUserid+"' and signtype=1 and rownum=1";
                            rs_getKey.execute(strGetId);
                            while (rs_getKey.next()) {
                                strId = rs_getKey.getString("id");
                            }
                            RecordSet rs_update_detail = new RecordSet();
                            String str_update_detail = "update kq_format_detail set signinid='" + strId + "',signdate='" + strBegin + "',signintime='" + strSignintime + "',attendancemins=480,belatemins=0,graveBeLateMins=0,leaveearlymins=0,graveLeaveEarlyMins=0,absenteeismmins=0,forgotcheckMins=0 where kqdate='" + strBegin + "' and resourceid='" + strUserid + "'";
                            rs_update_detail.execute(str_update_detail);
                            msg.put("更新签到明细表Sql：", str_update_detail);
                        }
                        String strOutId = "";
                        String strIsExists_out = "select * from hrmschedulesign where signdate='" + strkqdate + "' and signtype=2 and userid='"+strUserid+"'";
                        RecordSet rs_isexists_out = new RecordSet();
                        rs_isexists_out.execute(strIsExists_out);
                        while (rs_isexists_out.next()){
                            strOutId=rs_isexists_out.getString("id");
                        }
                        if(strSignouttime.equals("")){
                            if ("on".equals(strCheck)) {
                                if (!"".equals(strTime_end)) {
                                    strSignouttime = strTime_end;
                                    if (rs_isexists_out.getCounts() < 1) {
                                        String strInsertSignOut = "INSERT into hrmschedulesign (userid,usertype,SIGNTYPE,signdate,signtime,CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,BELONGDATE,MEMO)" +
                                                "SELECT userid,usertype,2,signdate,'" + strSignouttime + "',CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,BELONGDATE,MEMO FROM hrmschedulesign WHERE USERID='" + strUserid + "' AND signdate='" + strkqdate + "' AND signtype=1";
                                        msg.put("插入签退sql：", strInsertSignOut);
                                        RecordSet rs_signout = new RecordSet();
                                        rs_signout.execute(strInsertSignOut);
                                        RecordSet rs_getKey_out = new RecordSet();
                                        String strId = "";
                                        String strGetId = "select id from hrmschedulesign where signdate='" + strBegin + "' and signtype=2 and userid='"+strUserid+"' and rownum=1";
                                        rs_getKey_out.execute(strGetId);
                                        while (rs_getKey_out.next()) {
                                            strId = rs_getKey_out.getString("id");
                                        }
                                        RecordSet rs_update_detail2 = new RecordSet();
                                        String str_update_detail2 = "update kq_format_detail set signoutid='" + strId + "',signdate='" + strBegin + "',signintime='" + strSignouttime + "',attendancemins=480,belatemins=0,graveBeLateMins=0,leaveearlymins=0,graveLeaveEarlyMins=0,absenteeismmins=0,forgotcheckMins=0 where kqdate='" + strkqdate + "' and resourceid='" + strUserid + "'";
                                        rs_update_detail2.execute(str_update_detail2);
                                        msg.put("签退参数不为空时更新签到明细表Sql：", str_update_detail2);
                                        msg.put("执行状态：", "插入签退记录成功！");
                                    }
                                    String inTime = strkqdate + " " + strSignintime;
                                    String outTime = strkqdate + " " + strSignouttime;

                                    //实际签到时间差
                                    long timeDiffer = Math.round(getTime(inTime, outTime));
                                    long timeDiffer2 = Math.round(getTime(strkqdate + " " + "09:00:00",inTime));
                                    String strInfo = "{\"shiftRule\":{\"shift_type\":\"lateinlateout\",\"shift_endworktime\":\"19:00\",\"shift_beginworktime\":\"10:00\"" +
                                            ",\"shift_enddate\":\"" + strkqdate + "\",\"shift_begindate\":\"" + strkqdate + "\"}" +
                                            ",\"logList\":[\"" + strkqdate + " 上班时间是:" + strkqdate + " 09:00:00,签到时间是:" + inTime + ",晚到" + timeDiffer2 + "分钟, 开启了晚到晚走,相对时间,晚到可以晚走，弹性限额60分钟,满足了晚到,所以" + strkqdate + "的上班时间和下班时间变化了,上班时间变成了:" + strkqdate + " 10:00,下班时间变成了:" + strkqdate + " 19:00\"]}";
                                    //考勤明细表
                                    String strUpdate_detail = "UPDATE kq_format_detail SET signinid='" + strIntId + "',signoutid='" + strOutId + "',otherinfo='" + strInfo + "',signindate='" + strkqdate + "',signintime='" + strSignintime + "',signmins='" + timeDiffer + "' where resourceid=" + strUserid + " AND kqdate='" + strkqdate + "'";
                                    RecordSet rs_Update2 = new RecordSet();
                                    rs_Update2.execute(strUpdate_detail);
                                    msg.put("更新考勤明细表Sql2：", strUpdate_detail);
                                    msg.put("执行状态：", "成功!");
                                }else{
                                    msg.put("下班打卡数据插入未执行：", "请输入下班打卡时间!");
                                }

                            } else {
                                msg.put("下班打卡数据插入未执行：", "请输入下班打卡时间!");
                            }
                        }else {
                            if ("on".equals(strCheck)) {
                                if (!"".equals(strTime_end)) {
                                    strSignouttime = strTime_end;
                                    if (rs_isexists_out.getCounts() < 1) {
                                        String strInsertSignOut = "INSERT into hrmschedulesign (userid,usertype,SIGNTYPE,signdate,signtime,CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,BELONGDATE,MEMO)" +
                                                "SELECT userid,usertype,2,signdate,'" + strSignouttime + "',CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,BELONGDATE,MEMO FROM hrmschedulesign WHERE USERID='" + strUserid + "' AND signdate='" + strkqdate + "' AND signtype=1";
                                        msg.put("插入签退sql：", strInsertSignOut);
                                        RecordSet rs_signout = new RecordSet();
                                        rs_signout.execute(strInsertSignOut);
                                        RecordSet rs_getKey_out = new RecordSet();
                                        String strId = "";
                                        String strGetId = "select id from hrmschedulesign where signdate='" + strBegin + "' and userid='"+strUserid+"' and signtype=2 and rownum=1";
                                        rs_getKey_out.execute(strGetId);
                                        while (rs_getKey_out.next()) {
                                            strId = rs_getKey_out.getString("id");
                                        }
                                        RecordSet rs_update_detail2 = new RecordSet();
                                        String str_update_detail2 = "update kq_format_detail set signoutid='" + strId + "',signdate='" + strBegin + "',signintime='" + strSignouttime + "',attendancemins=480,belatemins=0,graveBeLateMins=0,leaveearlymins=0,graveLeaveEarlyMins=0,absenteeismmins=0,forgotcheckMins=0 where kqdate='" + strkqdate + "' and resourceid='" + strUserid + "'";
                                        rs_update_detail2.execute(str_update_detail2);
                                        msg.put("更新签到明细表Sql：", str_update_detail2);
                                        msg.put("执行状态：", "插入签退记录成功！");
                                    }
                                }
                                String inTime = strkqdate + " " + strSignintime;
                                String outTime = strkqdate + " " + strSignouttime;
                                //实际签到时间差
                                long timeDiffer = Math.round(getTime(inTime, outTime));
                                long timeDiffer2 = Math.round(getTime(strkqdate + " " + "09:00:00",inTime));
                                String strInfo = "{\"shiftRule\":{\"shift_type\":\"lateinlateout\",\"shift_endworktime\":\"19:00\",\"shift_beginworktime\":\"10:00\"" +
                                        ",\"shift_enddate\":\"" + strkqdate + "\",\"shift_begindate\":\"" + strkqdate + "\"}" +
                                        ",\"logList\":[\"" + strkqdate + " 上班时间是:" + strkqdate + " 09:00:00,签到时间是:" + inTime + ",晚到" + timeDiffer2 + "分钟, 开启了晚到晚走,相对时间,晚到可以晚走，弹性限额60分钟,满足了晚到,所以" + strkqdate + "的上班时间和下班时间变化了,上班时间变成了:" + strkqdate + " 10:00,下班时间变成了:" + strkqdate + " 19:00\"]}";
                                //考勤明细表
                                String strUpdate_detail = "UPDATE kq_format_detail SET otherinfo='" + strInfo + "',signindate='" + strkqdate + "',signintime='" + strSignintime + "',signmins='" + timeDiffer + "' where resourceid=" + strUserid + " AND kqdate='" + strkqdate + "'";
                                RecordSet rs_Update2 = new RecordSet();
                                rs_Update2.execute(strUpdate_detail);
                                msg.put("更新考勤明细表的Sql：", strUpdate_detail);
                                msg.put("执行状态：", "成功!");
                            } else {
                                msg.put("下班打卡未执行：", "请输入下班打卡时间!");
                            }
                        }

                    } else {//签到时间为空时
                        String strIsExists = "select * from hrmschedulesign where signdate='" + strBegin + "' and signtype=1";
                        RecordSet rs_isexists = new RecordSet();
                        rs_isexists.execute(strIsExists);
                        String strSignInId="";
                        while (rs_isexists.next()){
                            strSignInId=rs_isexists.getString("id");
                        }

                        if (rs_isexists.getCounts()>0) {
                            if(strSignouttime.equals("")){
                                if ("on".equals(strCheck)) {
                                    if (!"".equals(strTime_end)) {
                                        strSignouttime = strTime_end;
                                        String strIsExists_out = "select * from hrmschedulesign where signdate='" + strkqdate + "' and userid='"+strUserid+"' and signtype=2";
                                        RecordSet rs_isexists_out = new RecordSet();
                                        rs_isexists_out.execute(strIsExists_out);
                                        if (rs_isexists_out.getCounts() < 1) {
                                            String strInsertSignOut = "INSERT into hrmschedulesign (userid,usertype,SIGNTYPE,signdate,signtime,CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,BELONGDATE,MEMO)" +
                                                    "SELECT userid,usertype,2,signdate,'" + strSignouttime + "',CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,BELONGDATE,MEMO FROM hrmschedulesign WHERE USERID='" + strUserid + "' AND signdate='" + strkqdate + "' AND signtype=1";
                                            msg.put("插入签退sql：", strInsertSignOut);
                                            RecordSet rs_signout = new RecordSet();
                                            rs_signout.execute(strInsertSignOut);
                                            RecordSet rs_getKey_out = new RecordSet();
                                            String strId = "";
                                            String strGetId = "select id from hrmschedulesign where signdate='" + strBegin + "' and userid='"+strUserid+"' and signtype=2 and rownum=1";
                                            rs_getKey_out.execute(strGetId);
                                            while (rs_getKey_out.next()) {
                                                strId = rs_getKey_out.getString("id");
                                            }
                                            RecordSet rs_update_detail2 = new RecordSet();
                                            String str_update_detail2 = "update kq_format_detail set signinid='"+strSignInId+"',signoutid='" + strId + "',signdate='" + strBegin + "',signintime='" + strSignouttime + "',attendancemins=480,belatemins=0,graveBeLateMins=0,leaveearlymins=0,graveLeaveEarlyMins=0,absenteeismmins=0,forgotcheckMins=0 where kqdate='" + strkqdate + "' and resourceid='" + strUserid + "'";
                                            rs_update_detail2.execute(str_update_detail2);
                                            msg.put("签到时间参数为空时更新签到明细表Sql：", str_update_detail2);
                                            msg.put("执行状态：", "插入签退记录成功！");
                                        }
                                        String inTime = strkqdate + " " + strSignintime;
                                        String outTime = strkqdate + " " + strSignouttime;
                                        //实际签到时间差
                                        long timeDiffer = Math.round(getTime(inTime, outTime));
                                        long timeDiffer2 = Math.round(getTime(strkqdate + " " + "09:00:00",inTime));
                                        String strInfo = "{\"shiftRule\":{\"shift_type\":\"lateinlateout\",\"shift_endworktime\":\"19:00\",\"shift_beginworktime\":\"10:00\"" +
                                                ",\"shift_enddate\":\"" + strkqdate + "\",\"shift_begindate\":\"" + strkqdate + "\"}" +
                                                ",\"logList\":[\"" + strkqdate + " 上班时间是:" + strkqdate + " 09:00:00,签到时间是:" + inTime + ",晚到" + timeDiffer2 + "分钟, 开启了晚到晚走,相对时间,晚到可以晚走，弹性限额60分钟,满足了晚到,所以" + strkqdate + "的上班时间和下班时间变化了,上班时间变成了:" + strkqdate + " 10:00,下班时间变成了:" + strkqdate + " 19:00\"]}";
                                        //考勤明细表
                                        String strUpdate_detail = "UPDATE kq_format_detail SET signinid='"+strSignInId+"',otherinfo='" + strInfo + "',signindate='" + strkqdate + "',signintime='" + strSignintime + "',signmins='" + timeDiffer + "' where resourceid=" + strUserid + " AND kqdate='" + strkqdate + "'";
                                        RecordSet rs_Update2 = new RecordSet();
                                        rs_Update2.execute(strUpdate_detail);
                                        msg.put("签到时间为空时更新考勤明细表Sql：", strUpdate_detail);
                                        msg.put("执行状态：", "成功!");
                                    }else{
                                        msg.put("下班打卡数据插入未执行：", "请输入下班打卡时间!");
                                    }

                                } else {
                                    msg.put("下班打卡数据插入未执行：", "请输入下班打卡时间!");
                                }
                            }else {
                                if ("on".equals(strCheck)) {
                                    if (!"".equals(strTime_end)) {
                                        strSignouttime = strTime_end;
                                        String strIsExists_out = "select * from hrmschedulesign where signdate='" + strkqdate + "' and userid='"+strUserid+"' and signtype=2";
                                        RecordSet rs_isexists_out = new RecordSet();
                                        rs_isexists_out.execute(strIsExists_out);
                                        if (rs_isexists_out.getCounts() < 1) {
                                            String strInsertSignOut = "INSERT into hrmschedulesign (userid,usertype,SIGNTYPE,signdate,signtime,CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,BELONGDATE,MEMO)" +
                                                    "SELECT userid,usertype,2,signdate,'" + strSignouttime + "',CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,BELONGDATE,MEMO FROM hrmschedulesign WHERE USERID='" + strUserid + "' AND signdate='" + strkqdate + "' AND signtype=1";
                                            msg.put("插入签退sql：", strInsertSignOut);
                                            RecordSet rs_signout = new RecordSet();
                                            rs_signout.execute(strInsertSignOut);
                                            RecordSet rs_getKey_out = new RecordSet();
                                            String strId = "";
                                            String strGetId = "select id from hrmschedulesign where signdate='" + strBegin + "' and signtype=2 and rownum=1";
                                            rs_getKey_out.execute(strGetId);
                                            while (rs_getKey_out.next()) {
                                                strId = rs_getKey_out.getString("id");
                                            }
                                            RecordSet rs_update_detail2 = new RecordSet();
                                            String str_update_detail2 = "update kq_format_detail set signoutid='" + strId + "',signdate='" + strBegin + "',signintime='" + strSignouttime + "',attendancemins=480,belatemins=0,graveBeLateMins=0,leaveearlymins=0,graveLeaveEarlyMins=0,absenteeismmins=0,forgotcheckMins=0 where kqdate='" + strkqdate + "' and resourceid='" + strUserid + "'";
                                            rs_update_detail2.execute(str_update_detail2);
                                            msg.put("签退时间不为空时更新签到明细表Sql：", str_update_detail2);
                                            msg.put("执行状态：", "插入签退记录成功！");
                                        }
                                    }
                                    String inTime = strkqdate + " " + strSignintime;
                                    String outTime = strkqdate + " " + strSignouttime;
                                    //实际签到时间差
                                    long timeDiffer = Math.round(getTime(inTime, outTime));
                                    long timeDiffer2 = Math.round(getTime(strkqdate + " " + "09:00:00",inTime));
                                    String strInfo = "{\"shiftRule\":{\"shift_type\":\"lateinlateout\",\"shift_endworktime\":\"19:00\",\"shift_beginworktime\":\"10:00\"" +
                                            ",\"shift_enddate\":\"" + strkqdate + "\",\"shift_begindate\":\"" + strkqdate + "\"}" +
                                            ",\"logList\":[\"" + strkqdate + " 上班时间是:" + strkqdate + " 09:00:00,签到时间是:" + inTime + ",晚到" + timeDiffer2 + "分钟, 开启了晚到晚走,相对时间,晚到可以晚走，弹性限额60分钟,满足了晚到,所以" + strkqdate + "的上班时间和下班时间变化了,上班时间变成了:" + strkqdate + " 10:00,下班时间变成了:" + strkqdate + " 19:00\"]}";
                                    //考勤明细表
                                    String strUpdate_detail = "UPDATE kq_format_detail SET signinid='"+strSignInId+"',otherinfo='" + strInfo + "',signindate='" + strkqdate + "',signintime='" + strSignintime + "',signmins='" + timeDiffer + "' where resourceid=" + strUserid + " AND kqdate='" + strkqdate + "'";
                                    RecordSet rs_Update2 = new RecordSet();
                                    rs_Update2.execute(strUpdate_detail);
                                    msg.put("更新考勤明细表Sql：", strUpdate_detail);
                                    msg.put("执行状态：", "成功!");
                                } else {
                                    msg.put("下班打卡未执行：", "请输入下班打卡时间!");
                                }
                            }
                        }else {
                            msg.put("未执行：", "系统不存在上班打卡时间记录,请输入上班打卡时间！");
                        }
                    }

                }else{
                    if (!strTime_start.equals("")) {
                        strSignintime = strTime_start;
                    }
                    if (Integer.parseInt(strCdmins) > 0||Integer.parseInt(strKgmins) > 0||Integer.parseInt(strLqmins) > 0) {//迟到
                        if (!strTime_start.equals("")) {
                            strSignintime = strTime_start;
                            if (!strSignouttime.equals("")) {
                                String inTime = strkqdate + " " + strSignintime;
                                String outTime = strkqdate + " " + strSignouttime;
                                //实际签到时间差
                                long timeDiffer = Math.round(getTime(inTime, outTime));
                                double hoursDiffer = timeDiffer / 480.00;
                                String hoursDif = String.format("%.2f", hoursDiffer);
                                long timeDiffer2 = Math.round(getTime(strkqdate + " " + "09:00:00",inTime));
                                String strUpdateSign = "UPDATE hrmschedulesign SET isincom=1,signtime='" + strSignintime + "' where userid='" + strUserid + "' and signtype=1 and signdate='" + strkqdate + "'";
                                RecordSet rs_Update = new RecordSet();
                                rs_Update.execute(strUpdateSign);
                                msg.put("更新打卡签到Sql：", strUpdateSign);
                                RecordSet rs = new RecordSet();
                                String strInfo = "{\"shiftRule\":{\"shift_type\":\"lateinlateout\",\"shift_endworktime\":\"19:00\",\"shift_beginworktime\":\"10:00\"" +
                                        ",\"shift_enddate\":\"" + strkqdate + "\",\"shift_begindate\":\"" + strkqdate + "\"}" +
                                        ",\"logList\":[\"" + strkqdate + " 上班时间是:" + strkqdate + " 09:00:00,签到时间是:" + inTime + ",晚到" + timeDiffer2 + "分钟, 开启了晚到晚走,相对时间,晚到可以晚走，弹性限额60分钟,满足了晚到,所以" + strkqdate + "的上班时间和下班时间变化了,上班时间变成了:" + strkqdate + " 10:00,下班时间变成了:" + strkqdate + " 19:00\"]}";
                                String strUpdate = "UPDATE kq_format_detail SET otherinfo='" + strInfo + "',signintime='" + strSignintime + "',attendancemins=480,belatemins=0,graveBeLateMins=0,leaveearlymins=0,graveLeaveEarlyMins=0,absenteeismmins=0,forgotcheckMins=0,signmins=" + timeDiffer + ",FORGOTBEGINWORKCHECKMINS=0 WHERE kqdate='" + strkqdate + "' AND resourceid='" + strUserid + "'";
                                rs.execute(strUpdate);
                                msg.put("更新考勤明细表Sql：", strUpdate);
                                //考勤统计表
                                String strUpdate_toal = "UPDATE kq_format_total SET  FORGOTBEGINWORKCHECK=0,attendancedays=1,attendancemins=480,belate=0,belatemins=0,signmins='" + timeDiffer + "',signdays='" + hoursDif + "' where resourceid=" + strUserid + " AND kqdate='" + strkqdate + "'";
                                RecordSet rs_Update2 = new RecordSet();
                                rs_Update2.execute(strUpdate_toal);
                                msg.put("考勤统计表Sql：", strUpdate_toal);
                                msg.put("执行状态：", "更新成功！");
                            } else {
                                if ("on".equals(strCheck)) {
                                    if (!"".equals(strTime_end)) {
                                        strSignouttime = strTime_end;
                                        String inTime = strkqdate + " " + strSignintime;
                                        String outTime = strkqdate + " " + strSignouttime;
                                        //实际签到时间差
                                        long timeDiffer = Math.round(getTime(inTime, outTime));
                                        double hoursDiffer = timeDiffer / 480.00;
                                        String hoursDif = String.format("%.2f", hoursDiffer);
                                        long timeDiffer2 = Math.round(getTime(strkqdate + " " + "09:00:00",inTime));
                                        String strIsExists_out = "select * from hrmschedulesign where signdate='" + strkqdate + "' and signtype=2";
                                        RecordSet rs_isexists_out = new RecordSet();
                                        rs_isexists_out.execute(strIsExists_out);
                                        if (rs_isexists_out.getCounts() < 1) {
                                            String strInsertSignOut = "INSERT into hrmschedulesign (userid,usertype,SIGNTYPE,signdate,signtime,CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,BELONGDATE,MEMO)" +
                                                    "SELECT userid,usertype,2,signdate,'" + strSignouttime + "',CLIENTADDRESS,ISINCOM,SIGNFROM,LONGITUDE,LATITUDE,ADDR,ISIMPORT,SUUID,TIMEZONE,BELONGDATE,MEMO FROM hrmschedulesign WHERE USERID='" + strUserid + "' AND signdate='" + strkqdate + "' AND signtype=1";
                                            msg.put("插入签退sql：", strInsertSignOut);
                                            RecordSet rs_signout = new RecordSet();
                                            rs_signout.execute(strInsertSignOut);

                                            RecordSet rs_getKey = new RecordSet();
                                            String strId = "";
                                            String strGetId = "select id from hrmschedulesign where signdate='" + strBegin + "' and  userid='"+strUserid+"' and signtype=2 and rownum=1";
                                            rs_getKey.execute(strGetId);
                                            while (rs_getKey.next()) {
                                                strId = rs_getKey.getString("id");
                                            }
                                            String strInfo = "{\"shiftRule\":{\"shift_type\":\"lateinlateout\",\"shift_endworktime\":\"19:00\",\"shift_beginworktime\":\"10:00\"" +
                                                    ",\"shift_enddate\":\"" + strkqdate + "\",\"shift_begindate\":\"" + strkqdate + "\"}" +
                                                    ",\"logList\":[\"" + strkqdate + " 上班时间是:" + strkqdate + " 09:00:00,签到时间是:" + inTime + ",晚到" + timeDiffer2 + "分钟, 开启了晚到晚走,相对时间,晚到可以晚走，弹性限额60分钟,满足了晚到,所以" + strkqdate + "的上班时间和下班时间变化了,上班时间变成了:" + strkqdate + " 10:00,下班时间变成了:" + strkqdate + " 19:00\"]}";
                                            RecordSet rs_update_detail = new RecordSet();
                                            String str_update_detail = "update kq_format_detail set otherinfo='" + strInfo + "',signintime='" + strSignintime + "',signoutid='" + strId + "',signoutdate='" + strkqdate + "',signouttime='" + strSignouttime + "',attendancemins=480,belatemins=0,graveBeLateMins=0,leaveearlymins=0,graveLeaveEarlyMins=0,absenteeismmins=0,forgotcheckMins=0 where kqdate='" + strkqdate + "' and resourceid='" + strUserid + "'";
                                            rs_update_detail.execute(str_update_detail);
                                            msg.put("更新签到明细表Sql：", str_update_detail);
                                            //考勤统计表
                                            String strUpdate_toal = "UPDATE kq_format_total SET  FORGOTBEGINWORKCHECK=0,attendancedays=1,attendancemins=480,belate=0,belatemins=0,signmins='" + timeDiffer + "',signdays='" + hoursDif + "' where resourceid=" + strUserid + " AND kqdate='" + strkqdate + "'";
                                            RecordSet rs_Update2 = new RecordSet();
                                            rs_Update2.execute(strUpdate_toal);
                                            msg.put("考勤统计表Sql：", strUpdate_toal);
                                            msg.put("执行状态：", "插入成功！");
                                        }else{
                                            RecordSet rs_getKey = new RecordSet();
                                            String strId = "";
                                            String strGetId = "select id from hrmschedulesign where signdate='" + strBegin + "' and  userid='"+strUserid+"' and signtype=2 and rownum=1";
                                            rs_getKey.execute(strGetId);
                                            while (rs_getKey.next()) {
                                                strId = rs_getKey.getString("id");
                                            }
                                            String strInfo = "{\"shiftRule\":{\"shift_type\":\"lateinlateout\",\"shift_endworktime\":\"19:00\",\"shift_beginworktime\":\"10:00\"" +
                                                    ",\"shift_enddate\":\"" + strkqdate + "\",\"shift_begindate\":\"" + strkqdate + "\"}" +
                                                    ",\"logList\":[\"" + strkqdate + " 上班时间是:" + strkqdate + " 09:00:00,签到时间是:" + inTime + ",晚到" + timeDiffer2 + "分钟, 开启了晚到晚走,相对时间,晚到可以晚走，弹性限额60分钟,满足了晚到,所以" + strkqdate + "的上班时间和下班时间变化了,上班时间变成了:" + strkqdate + " 10:00,下班时间变成了:" + strkqdate + " 19:00\"]}";
                                            RecordSet rs_update_detail = new RecordSet();
                                            String str_update_detail = "update kq_format_detail set otherinfo='" + strInfo + "',signintime='" + strSignintime + "',signoutid='" + strId + "',signoutdate='" + strkqdate + "',signouttime='" + strSignouttime + "',attendancemins=480,belatemins=0,graveBeLateMins=0,leaveearlymins=0,graveLeaveEarlyMins=0,absenteeismmins=0,forgotcheckMins=0 where kqdate='" + strkqdate + "' and resourceid='" + strUserid + "'";
                                            rs_update_detail.execute(str_update_detail);
                                            msg.put("更新签到明细表Sql：", str_update_detail);
                                            //考勤统计表
                                            String strUpdate_toal = "UPDATE kq_format_total SET  FORGOTBEGINWORKCHECK=0,attendancedays=1,attendancemins=480,belate=0,belatemins=0,signmins='" + timeDiffer + "',signdays='" + hoursDif + "' where resourceid=" + strUserid + " AND kqdate='" + strkqdate + "'";
                                            RecordSet rs_Update2 = new RecordSet();
                                            rs_Update2.execute(strUpdate_toal);
                                            msg.put("考勤统计表Sql：", strUpdate_toal);
                                            msg.put("执行状态：", "插入成功！");
                                        }
                                    } else {
                                        msg.put("未执行：", "请输入下班打卡时间！");
                                    }
                                }else{
                                    msg.put("未执行：", "下班打卡时间为空,请选择签退插入选择！");
                                }
                            }
                        }else{
                            msg.put("未执行：", "存在迟到记录,请输入上班打卡时间！");
                        }
                    }
                }
            }
            if (!"".equals(strRequestids)) {
                String strDelete = "delete FROM workflow_requestbase  WHERE requestid in(" + strRequestids + ")";
                RecordSet rs = new RecordSet();
                rs.execute(strDelete);
            }
        }else{
            String strGetGh="select WORKCODE from hrmresource where id='"+strUserid+"'";
            String strGh="";
            RecordSet rs= new RecordSet();
            rs.execute(strGetGh);
            while (rs.next()){
                strGh=rs.getString("WORKCODE");
            }
            if(!"".equals(strGh)){
                String strSqls = "SELECT * FROM (select a.* from uf_v8record a where a.dkdate>='" + strBegin + "' and a.dkdate<='" + strEnd + "' and a.gh='" + strGh + "' ORDER BY dksj desc) WHERE ROWNUM=1 " +
                        " union SELECT * FROM (select a.* from uf_v8record a where a.dkdate>='" + strBegin + "' and a.dkdate<='" + strEnd + "' and a.gh='" + strGh + "' ORDER BY dksj asc) WHERE ROWNUM=1";
                RecordSet rs_v8=new RecordSet();
                rs_v8.execute(strSqls);
                while (rs_v8.next()){
                    String strGhs=rs_v8.getString("gh");
                    //手机号
                    String strPhone = rs_v8.getString("phone");
                    //打卡门店
                    String strDkmd = rs_v8.getString("dkmd");
                    //打卡日期时间
                    String strDksj = rs_v8.getString("dksj");
                    //打卡日期
                    String strDkDate = rs_v8.getString("dkdate");
                    //打卡时间
                    String strDkTime = rs_v8.getString("dktime");
                    //打卡IP
                    String strDkIp =  rs_v8.getString("dkip");
                    //打卡机器MAC地址
                    String strMac = rs_v8.getString("macaddress");
                    //门店地址
                    String strMdAddress =rs_v8.getString("mdaddress");
                    //打卡类型
                    String strSignTYpe="";
                    String dateFlag=strDkDate+" 12:00:00";
                    Date date2 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateFlag);
                    Date date1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(strDksj);
                    if (date1.compareTo(date2)==-1) {//打卡时间早于12点前记录为上班
                        strSignTYpe="1";
                        RecordSetTrans updateTrans = new RecordSetTrans();
                        RecordSet rs_exists=new RecordSet();
                        String strExists="select * from hrmschedulesign where signdate='"+strDkDate+"' and signtype='"+strSignTYpe+"' and userid='"+strUserid+"'";
                        rs_exists.execute(strExists);
                        if(rs_exists.getCounts()>0) {
                            String strUpdate = "update hrmschedulesign set isincom=1,signtime='" + strDkTime + "',clientaddress='" + strDkIp + "',signfrom='V8',addr='" + strMdAddress + "' where userid='" + strUserid + "' " +
                                    " and signdate='" + strDkDate + "' and signtype='" + strSignTYpe + "' ";
                            try {
                                updateTrans.setAutoCommit(false);
                                updateTrans.executeSql(strUpdate);
                                updateTrans.commit();
                                String strUpSql = "update uf_xdjhb set sfkq='Y' where xdrq='" + strDkDate + "' and ygh='" + strGh + "'";
                                //log.info("更新是否已考勤字段sql:"+strUpSql);
                                RecordSet rsUpdate = new RecordSet();
                                rsUpdate.execute(strUpdate);
                                msg.put("上班打卡记录：", "更新成功！sql:"+strUpdate);
                            } catch (Exception ex) {
                                ex.printStackTrace();
                                updateTrans.rollback();
                                msg.put("上班打卡记录：", "更新失败！" + ex.toString());
                            }
                        }else{
                            String strInsert="INSERT INTO hrmschedulesign (userid,usertype,signtype,signdate,signtime,clientaddress,signfrom,addr,belongdate,isincom)VALUES"+
                                    "("+strUserid+",1,"+strSignTYpe+",'"+strDkDate+"','"+strDkTime+"','"+strDkIp+"','V8','"+strMdAddress+"','"+strDkDate+"',1)";
                            RecordSet rsInsert = new RecordSet();
                            rsInsert.execute(strInsert);
                            msg.put("上班打卡记录：", "插入成功！sql:"+strInsert);
                        }
                    }else{
                        strSignTYpe="2";
                        RecordSet rs_exists=new RecordSet();
                        String strExists="select * from hrmschedulesign where signdate='"+strDkDate+"' and signtype='"+strSignTYpe+"' and  userid='"+strUserid+"'";
                        rs_exists.execute(strExists);
                        if(rs_exists.getCounts()>0) {
                            RecordSetTrans updateTrans = new RecordSetTrans();
                            String strUpdate = "update hrmschedulesign set isincom=1,signtime='" + strDkTime + "',clientaddress='" + strDkIp + "',signfrom='V8',addr='" + strMdAddress + "' where userid='" + strUserid + "' " +
                                    " and signdate='" + strDkDate + "' and signtype='" + strSignTYpe + "' ";
                            try {
                                updateTrans.setAutoCommit(false);
                                updateTrans.executeSql(strUpdate);
                                updateTrans.commit();
                                String strUpSql = "update uf_xdjhb set sfkq='Y' where xdrq='" + strDkDate + "' and ygh='" + strGh + "'";
                                //log.info("更新是否已考勤字段sql:"+strUpSql);
                                RecordSet rsUpdate = new RecordSet();
                                rsUpdate.execute(strUpdate);
                                msg.put("下班打卡记录：", "插入成功！sql:"+strUpdate);
                            } catch (Exception ex) {
                                ex.printStackTrace();
                                updateTrans.rollback();
                                msg.put("下班打卡记录：", "插入失败！" + ex.toString());
                            }
                        }else{
                            String strInsert="INSERT INTO hrmschedulesign (userid,usertype,signtype,signdate,signtime,clientaddress,signfrom,addr,belongdate,isincom)VALUES"+
                                    "("+strUserid+",1,"+strSignTYpe+",'"+strDkDate+"','"+strDkTime+"','"+strDkIp+"','V8','"+strMdAddress+"','"+strDkDate+"',1)";
                            RecordSet rsInsert = new RecordSet();
                            rsInsert.execute(strInsert);
                            msg.put("上班打卡记录：", "插入成功！sql:"+strInsert);
                        }
                    }
                }
            }
        }

    }
%>
<html>
<head>
    <LINK href="/css/Weaver_wev8.css" type=text/css rel=STYLESHEET>
    <LINK href="/js/hrm/jquery.jsonview.css" type=text/css rel=STYLESHEET>
    <script src="/js/hrm/jquery.jsonview.js"></script>
    <%--    <script type="text/javascript" src="/cloudstore/resource/pc/jquery/jquery-1.8.3.min.js"></script>--%>
    <script>
        function btnSubmit() {
            if(frm.resourceid.value==""){
                alert("请选择人员！");
                return;
            }
            if(frm.resourceid.value.indexOf(",")!=-1){
                alert("人员必须为单选！");
                return;
            }
            if(frm.kqdate.value==""){
                alert("请选择日期！");
                return;
            }
            if (confirm("确认刷新数据吗")) {
                frm.cmd.value="update";
                frm.submit();
            }

        }
        jQuery(document).ready(function(){
            $("#json").JSONView($("#json").html(), { expand: true, nl2br: true, recursive_collapser: true });

        });
    </script>

</head>
<BODY>
<form name="frm" action="update_kqdata.jsp" method="post">
    <input type="hidden" name="cmd" value="">
    <div style="margin: auto">
        <table style="margin: 0 0 0 0">
            <tr style="height: 30px">
                <td>人员:</td>
                <td>
                    <brow:browser viewType="0"  name="resourceid" browserValue='<%=strUserid %>'

                                  browserOnClick=""

                                  browserUrl="<%=new BrowserComInfo().getBrowserurl("17") %>"

                                  hasInput="true" isSingle="false" hasBrowser = "true" isMustInput='1' linkUrl="javascript:openhrm($id$)"

                                  completeUrl="/data.jsp?type=17" width="265px"

                                  browserSpanValue='<%=Util.toScreen(ResourceComInfo.getResourcename(strUserid),user.getLanguage())%>'></brow:browser>
                </td>
                <td>-</td>
                <td>流程id：</td>
                <td>
                    <input name="requestid" class="inputstyle" type="text"/>
                </td>
            </tr>
            <tr style="height: 30px">
                <td>签到时间：</td>
                <td>
                    <input name="signTime" class="inputstyle" type="text"/>
                </td>
                <td>-</td>
                <td>签退时间：</td>
                <td>
                    <input name="signOutTime" class="inputstyle" type="text"/>
                </td>
            </tr>
            <tr style="height: 30px">
                <td>开始日期：</td>
                <td>
                    <BUTTON type="button" class=Calendar id=btnkqdate onClick="getDate(kqdatespan,kqdate)"></BUTTON>
                    <SPAN id=kqdatespan ><%=strBegin%></SPAN>
                    <input class=inputstyle type="hidden" name=kqdate value="<%=strBegin%>">
                </td>
                <td>-</td>
                <td>结束日期：</td>
                <td>
                    <BUTTON type="button" class=Calendar id=btnkqdateto onClick="getDate(kqdatetospan,kqdateto)"></BUTTON>
                    <SPAN id=kqdatetospan ><%=strEnd%></SPAN>
                    <input class=inputstyle type="hidden" name=kqdateto value="<%=strEnd%>">
                </td>
            </tr>
            <tr>
                <td>
                    签退插入选择
                </td>
                <td>
                    <input type="checkbox" name="check"/>
                </td>
                <td>
                    是否V8打卡
                </td>
                <td>
                    <input type="checkbox" name="siV8"/>
                </td>
                <td colspan="3" style="text-align: center;height: 50px;">
                    <input type="button" value="刷新数据" onClick="btnSubmit();"/>&nbsp;&nbsp;
                </td>
            </tr>
        </table>
        <div id="json" style="padding-left: 10px">
            执行结果：<%=JSONObject.toJSONString(msg, SerializerFeature.DisableCircularReferenceDetect) %>
        </div>
    </div>
    <script language="javascript" defer="defer" src="/js/datetime_wev8.js"></script>
    <script language="javascript" defer="defer" src="/js/JSDateTime/WdatePicker_wev8.js"></script>
</form>

<%!
    private long getTime(String parse1, String parse2) {
        SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        long NTime = 0;
        long OTime = 0;
        long diff= 0;
        try {
            NTime = df.parse(parse1).getTime();
            OTime = df.parse(parse2).getTime();
            diff=((OTime-NTime)/1000)/60;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //从对象中拿到时间
        return diff;
    }
%>